package com.dl.mall.config.satoken;

import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpUtil;
import com.dl.mall.service.admin.AdminService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 自定义权限验证接口扩展
 * @author DL
 * @create 2022-04-07 10:44
 */
@Component    // 保证此类被SpringBoot扫描，完成Sa-Token的自定义权限验证扩展
public class StpInterfaceImpl implements StpInterface {


    @Resource
    private AdminService adminService;

    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        //System.out.println("loginId = " + loginId);
        //System.out.println("loginType = " + loginType);
        // 本list仅做模拟，实际项目中要根据具体业务逻辑来查询权限
        List<String> list = new ArrayList<String>();
        list.add("user-add");
        list.add("user-delete");
        list.add("user-update");
        list.add("user-get");
        return list;
    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        //System.out.println("getRoleList loginId = " + loginId);
        // 本list仅做模拟，实际项目中要根据具体业务逻辑来查询角色
        Map<String, String> info = adminService.getRoleById((String) loginId);
        String roleName = info.get("role_name");

        return Arrays.asList(roleName.split(","));
    }

}

